package com.lakeworks.mapper;

import com.lakeworks.common.domain.channel.ProductInfo;
import com.lakeworks.domain.OprPickup;
import com.lakeworks.domain.vo.PickupFrame;
import com.lakeworks.domain.vo.PickupVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 入厂单Mapper接口
 *
 * @author lake
 * @date 2023-02-08
 */
public interface OprPickupMapper {
    @Select("select count(*) from opr_pickup where express_no = #{expressNo} and flag & 512 != 512")
    int selectByExpressNo(String expressNo);

    @Select("select * from opr_pickup where express_no = #{expressNo} and status = '1' and flag & 512 != 512 ")
    OprPickup selectByExpressNostate(String expressNo);

    /**
     * 查询入厂单
     *
     * @param id 入厂单主键
     * @return 入厂单
     */
    OprPickup selectOprPickupById(Long id);

    /**
     * 查询入厂单列表
     *
     * @param oprPickup 入厂单
     * @return 入厂单集合
     */
    List<OprPickup> selectOprPickupList(OprPickup oprPickup);



    List<OprPickup> selectPickupList(OprPickup oprPickup);
    /**
     * 新增入厂单
     *
     * @param oprPickup 入厂单
     * @return 结果
     */
    int insertOprPickup(OprPickup oprPickup);

    /**
     * 修改入厂单
     *
     * @param oprPickup 入厂单
     * @return 结果
     */
    int updateOprPickup(OprPickup oprPickup);


    int updatePickupReceived(OprPickup oprPickup);

    /**
     * 批量删除入厂单
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    int deleteOprPickupByIds(String[] ids);


    PickupVo selectPickupByExpressNo(String expressNo);

    OprPickup selectPickByExpressNo(String expressNo);


    int updatePickupStatus(OprPickup oprPickup);

    OprPickup queryByChannelIdAndChPickupNo(@Param("channelId") Long channelId, @Param("chPickupNo") String chPickupNo);

    @Select("SELECT opp.id 'pickupId',\n" +
            "GROUP_CONCAT(opt.status SEPARATOR ',') 'proStatus',\n" +
            "opt.frame_no 'frameNo',\n" +
            "oat.frame_no 'attFrameNo'\n" +
            "FROM opr_product opt\n" +
            "LEFT JOIN opr_pickup opp\n" +
            "ON opt.pickup_id = opp.id \n" +
            "LEFT JOIN opr_attachment oat\n" +
            "ON oat.id = ( \n" +
            "\tSELECT MAX(id) id FROM opr_attachment \n" +
            "\tWHERE opr_attachment.product_id = opt.id\n" +
            ")\n" +
            "WHERE opp.id = #{pickupId}\n" +
            "and opp.flag & 512 != 512\n" +
            "GROUP BY opp.id")
    PickupFrame queryPickupFrameVOById(@Param("pickupId") Long pickupId);

    @Select("SELECT om.code,om.name,\n" +
            "opt.barcode,opt.status \n" +
            "FROM opr_pickup opp\n" +
            "LEFT JOIN opr_merchandise om\n" +
            "ON om.pickup_id = opp.id\n" +
            "AND om.flag & 512 != 512\n" +
            "LEFT JOIN opr_product opt\n" +
            "ON opt.merchandise_id = om.id\n" +
            "AND opt.flag & 512 != 512\n" +
            "AND opt.companion_id IS NULL\n" +
            "WHERE opp.id = #{id}\n" +
            "AND opp.flag & 512 != 512")
    List<ProductInfo> pickupInfoById(@Param("id") Long id);

    @Select("SELECT om.code,om.name,\n" +
            "opt.barcode,opt.status \n" +
            "FROM opr_pickup opp\n" +
            "LEFT JOIN opr_merchandise om\n" +
            "ON om.pickup_id = opp.id\n" +
            "LEFT JOIN opr_product opt\n" +
            "ON opt.merchandise_id = om.id\n" +
            "WHERE opp.id = #{id}\n" +
            "AND opp.flag & 512 != 512\n" +
            "AND opt.barcode = #{barcode}")
    ProductInfo productInfoById(@Param("id") Long id, @Param("barcode") String barcode);


    List<OprPickup> selectPickupNotCheck(String orderNo);

    List<OprPickup> selectPickUpSignAndVerify();


    Integer selectTimeOutPickUp(Integer hours);

    @Select("SELECT opp.order_no FROM opr_product opt\n" +
            "LEFT JOIN opr_pickup opp\n" +
            "ON opt.pickup_id = opp.id\n" +
            "WHERE opt.barcode = #{barcode}")
    String selectOrderNoByBarcode(@Param("barcode") String barcode);


    int updateFlagById(OprPickup oprPickup);

    int deleteFlagById(OprPickup oprPickup);

    @Select("SELECT COUNT(opp.id)\n" +
            "FROM opr_pickup opp\n" +
            "WHERE opp.status = 4\n" +
            "AND opp.flag & 512 != 512\n" +
            "AND EXISTS (\n" +
            "\tSELECT 1 FROM opr_product\n" +
            "\tWHERE pickup_id = opp.id\n" +
            "\tAND STATUS != 13\n" +
            "\tAND flag & 512 != 512\n" +
            ")\n" +
            "UNION ALL\n" +
            "SELECT COUNT(id)\n" +
            "FROM opr_product\n" +
            "WHERE STATUS < 13\n" +
            "AND flag & 512 != 512\n" +
            "AND companion_id IS NULL")
    List<Integer> selectAtFactory();


    PickupVo selectPickupVoById(Long id);


   Integer selectVerifyCount(Integer hour);


   Map<String,Integer> selectNotReceivedCount(@Param("startHour")Integer startHour,@Param("endHour")Integer endHour);


   Map<String,Object> selectPickupCustomerCount(@Param("startDays")Integer startDays,@Param("endDays")Integer endDays);
}
